package com.cs4261.smartalarm.frontend;

import java.io.IOException;

import com.cs4261.smartalarm.R;
import com.cs4261.smartalarm.backend.Alarm;
import com.cs4261.smartalarm.backend.AlarmsDatabaseHelper;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AlarmActivity extends Activity {

	public Alarm alarm;
	public Uri alert;
	public Ringtone tone;
	public MediaPlayer mediaPlayer;
	public boolean flag;
	public long alarm_id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm);
		
		flag = false;
		alarm_id = getIntent().getLongExtra("alarm_id", -1);
		
		if(alarm_id == -1) {
			Toast.makeText(getApplicationContext(), "alarm id null", Toast.LENGTH_LONG).show();
		}
		
		AlarmsDatabaseHelper alDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alDBHelper.open();
		alarm = alDBHelper.getAlarmById(alarm_id);
		alDBHelper.close();
		
		
		TextView time_original = (TextView)findViewById(R.id.alarm_time_original);
		time_original.setText((alarm.getHour()%12 == 0 ? 12 : alarm.getHour()%12) + ":" + (alarm.getMinute() < 10 ? "0" + alarm.getMinute() : alarm.getMinute()) + (alarm.getHour() > 12 ? " PM" : " AM"));
		
		TextView time = (TextView)findViewById(R.id.alarm_time);
		time.setText((alarm.getHour()%12 == 0 ? 12 : alarm.getHour()%12) + ":" + (alarm.getMinute() < 10 ? "0" + alarm.getMinute() : alarm.getMinute() - 15) + (alarm.getHour() > 12 ? " PM" : " AM"));
		
		TextView name = (TextView)findViewById(R.id.alarm_name);
		name.setText(alarm.getName());
		
		alert = Uri.parse(alarm.getTone());
		
		if(RingtoneManager.getRingtone(getApplicationContext(), alert) == null) {
			alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
			if (RingtoneManager.getRingtone(getApplicationContext(), alert) == null) {
				// alert is null, using backup
				alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
				if (RingtoneManager.getRingtone(getApplicationContext(), alert) == null) {
					// alert backup is null, using 2nd backup
					alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
				}
			}
		}
		
		
		mediaPlayer = new MediaPlayer();
		try {
			mediaPlayer.setDataSource(this, alert);
			AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mediaPlayer.setLooping(true); 
				mediaPlayer.prepare();
				mediaPlayer.start(); 
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void stopAlarm(View view) {
		if(mediaPlayer != null) mediaPlayer.stop();
	}
	
	public void snoozeAlarm(View view) {
		if(mediaPlayer != null) mediaPlayer.stop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alarm, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}